{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0e4bdff",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 升级版--->增加循环,购物车"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad205201",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输出您的余额：100\n",
      "-----------【商品列表】----------\n",
      "商品编号：0,商品名称：book,商品的价格：30元\n",
      "商品编号：1,商品名称：pen,商品的价格：10元\n",
      "商品编号：2,商品名称：apple,商品的价格：3元\n",
      "商品编号：3,商品名称：banana,商品的价格：2元\n",
      "请输入正确的商品编号：2\n",
      "【apple】已经加入您的购物车，您的【账户余额】还有：97 元\n",
      "-----------【商品列表】----------\n",
      "商品编号：0,商品名称：book,商品的价格：30元\n",
      "商品编号：1,商品名称：pen,商品的价格：10元\n",
      "商品编号：2,商品名称：apple,商品的价格：3元\n",
      "商品编号：3,商品名称：banana,商品的价格：2元\n",
      "请输入正确的商品编号：2\n",
      "【apple】已经加入您的购物车，您的【账户余额】还有：94 元\n",
      "-----------【商品列表】----------\n",
      "商品编号：0,商品名称：book,商品的价格：30元\n",
      "商品编号：1,商品名称：pen,商品的价格：10元\n",
      "商品编号：2,商品名称：apple,商品的价格：3元\n",
      "商品编号：3,商品名称：banana,商品的价格：2元\n",
      "请输入正确的商品编号：3\n",
      "【banana】已经加入您的购物车，您的【账户余额】还有：92 元\n",
      "-----------【商品列表】----------\n",
      "商品编号：0,商品名称：book,商品的价格：30元\n",
      "商品编号：1,商品名称：pen,商品的价格：10元\n",
      "商品编号：2,商品名称：apple,商品的价格：3元\n",
      "商品编号：3,商品名称：banana,商品的价格：2元\n"
     ]
    }
   ],
   "source": [
    "goods_list = [\n",
    "    [\"book\",30],\n",
    "    [\"pen\",10],\n",
    "    [\"apple\",3],\n",
    "    [\"banana\",2]\n",
    "]\n",
    "users_shopping_car = []\n",
    "Alipay = int(input(\"请输出您的余额：\"))\n",
    "\n",
    "while True:#循环遍历商品\n",
    "    print(\"-----------【商品列表】----------\")\n",
    "    for i in range(len(goods_list)):\n",
    "        print(\"商品编号：%s,商品名称：%s,商品的价格：%d元\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "   \n",
    "    choice = input(\"请输入正确的商品编号：\")\n",
    "    if choice.isdigit():\n",
    "        choice = int(choice)\n",
    "#是正确的编号\n",
    "        if choice > len(goods_list):   \n",
    "            print(\"您选择的商品不存在\")\n",
    "        else:\n",
    "    #1.看一看用户钱够不够：Alipay VS goods_list[choice][1]\n",
    "            if Alipay > goods_list[choice][1]:\n",
    "                users_shopping_car.append(goods_list[choice])\n",
    "                Alipay = Alipay - goods_list[choice][1]\n",
    "                print(\"【%s】已经加入您的购物车，您的【账户余额】还有：%s 元\"%(goods_list[choice][0],Alipay))\n",
    "            else:\n",
    "                print(\"您的余额不足，请更换支付方式\")\n",
    "       \n",
    "    elif choice == \"q\":\n",
    "        print(\"用户购物车：\",user_shopping_car)\n",
    "        print(\"用户购物车：\",user_shopping_car)\n",
    "        break\n",
    "    else:\n",
    "        print(\"请输入商品编号\")\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f533523f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a9c826af",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
